package com.igoal.campaign.xmas2010.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.igoal.campaign.xmas2010.dao.Xmas2010DAO;
import com.igoal.campaign.xmas2010.dto.XmasWishDTO;
import com.igoal.campaign.xmas2010.entity.Xmas2010;
import com.igoal.dao.UserDAOExt;
import com.igoal.entity.User;
import com.igoal.entity.factory.EntityFactory;

@Service("xmasCardService")
public class CardServiceImpl implements CardService {

	@Qualifier("xmas2010DAO")
	private Xmas2010DAO xmas2010DAO;
	
	@Qualifier("userDAOExt")
	private UserDAOExt userDAOExt;
	
	@Autowired
	public void setUserDAOExt(UserDAOExt userDAOExt) {
		this.userDAOExt = userDAOExt;
	}

	@Autowired
	public void setXmas2010DAO(Xmas2010DAO xmas2010dao) {
		xmas2010DAO = xmas2010dao;
	}

	public XmasWishDTO getXmasWish(Long id) {
		Xmas2010 wish = xmas2010DAO.get(id);
		if (wish != null) {
			XmasWishDTO dto = new XmasWishDTO();
			dto.initFrom(wish);
			return dto;
		}
		return null;
	}
	
	public XmasWishDTO saveXmasWish(XmasWishDTO dto, String username){
		User user = (User) userDAOExt.loadUserByUsername(username);
		Xmas2010 wish = EntityFactory.createXmasWish(user, null, null, null);
		dto.copyAttributesTo(wish);
		wish = xmas2010DAO.save(wish);
		
		dto.initFrom(wish);
		return dto;
	}

}
